package com.colin.base.javaconcurrency.threadobjectclasscommonmethods;

/**
 * Created by Colin on 2020/3/4 15:03
 * email: colinzhaodong@gmail.com
 * desc: 展示wait和notify的基本用法
 * 1.研究代码执行顺序
 * 2.证明wait释放锁
 *
 * @author zhaod
 */
public class Wait {
	public static Object object = new Object();

	static class Thread1 extends Thread {

		@Override
		public void run() {
			synchronized (object) {
				System.out.println(Thread.currentThread().getName() + "开始执行了");
				try {
					object.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("线程" + Thread.currentThread().getName() + "获取到了锁。");
			}
		}
	}

	static class Thread2 extends Thread {

		@Override
		public void run() {
			synchronized (object) {
				object.notify();
				System.out.println("线程" + Thread.currentThread().getName() + "调用了notify()");
			}
		}
	}

	public static void main(String[] args) throws InterruptedException {
		Thread1 thread1 = new Thread1();
		Thread2 thread2 = new Thread2();
		thread1.start();
		Thread.sleep(200);
		thread2.start();
	}
}
